<?php
class luckydraw_model extends MY_Model
{
    private $table = 'blues_luckydraw';
    public function __construct()
    {
        parent::__construct();
    }
    public function get_luckydraw_by_id($id)
    {
        $this->db->where("id",$id);
        $query=$this->db->get($this->table);
        return $query->row_array();
    }
   
    public function get_luckydraw_list($data, $per_page, $limit, $filds="*")
    {
        $limit = " limit {$per_page},{$limit}";

		$sql = "SELECT * FROM blues_luckydraw WHERE isDel=0  ORDER BY onlinestatus desc, addtime DESC  {$limit}";
		$query = $this->db->query($sql);
        return $query->result_array();        
    }
    public function getLuckydrawCnt($data)
    {
        $sql = "SELECT count(*) cnt FROM blues_luckydraw WHERE isDel=0  ";
        $query = $this->db->query($sql);
        return $query->num_rows();        
    }
    public function add_luckydraw($data)
    {
        $res= $this->db->insert($this->table,$data);
        if($res){
            return $this->db->insert_id();
        }
        return FALSE;
    }
    public function edit_luckydraw($id,$data)
    {
        $this->db->where('id',$id);
        return $this->db->update($this->table,$data);
    }        
    //删除分类
    public function del_luckydraw($luckydrawid)
    {
		$this->db->where('id',$luckydrawid);
        return $this->db->update($this->table,array("isDel"=>1));
    }
  
   
                  //插入图书图片
    public function add_luckydraw_img($data = array()){
        $res = $this->db->insert("blues_luckydraw_attach",$data);
        return $res;
    }     
    //根据id修改图片
    public function edit_luckydraw_img($id,$data){
        $this->db->where("id",$id);
        $res = $this->db->update("blues_luckydraw_attach",$data);
        return $res;
    }

    public function editStatus($data = array()){

        $this->db->where("id",$data['id']);
        if($data['type'] == "onlinestatus"){
            $data = array(
                'onlinestatus'=>$data['onlinestatus']
            );
        }
        if($data['type'] == "isDel"){
            $data = array(
                'isDel'=>1
            );
        }

        $res = $this->db->update("blues_luckydraw",$data);
        return $res;
    }

     //获取图片列表
    public function get_img_list_byid($id){
        $this->db->where("luckydrawid",$id);
        $query = $this->db->get("blues_luckydraw_attach");
        return $query->result_array();
    }
    //根据id删除对应图片
    public function del_attach($id){
        $this->db->where("id",$id);
        $res = $this->db->delete("blues_luckydraw_attach");
        return $res;
    }
    public function editExpress($id,$data){
        $this->db->where("id",$id);
        $res = $this->db->update("blues_luckydraw_orders",$data);
        return $res;
    }



    public function getOrderList($data, $per_page, $limit, $filds="*"){
		$where = " ";

        if(!empty($data['starttime'])){
            $where .= " AND createtime >= {$data['starttime']}";
        }
        if(!empty($data['endtime'])){
            $data['endtime'] = $data['endtime']+3600*24;
            $where .= " AND createtime <= {$data['endtime']}";
        }
        if(!empty($data['keyword'])){
            $where .= " AND goodsName like '%{$data['keyword']}%'  OR mobile like '%{$data['keyword']}%' OR receiver like '%{$data['keyword']}%' or AccountID like '%{$data['keyword']}%'";
        }
         if(!empty($data['status'])){
            $data['status'] = $data['status'];
            $where .= " AND status = {$data['status']}";
        }
         if(!empty($data['gettype'])){
            $data['gettype'] = $data['gettype'];
            $where .= " AND gettype = {$data['gettype']}";
        }
        
        $limit = " limit {$per_page},{$limit}";

        $sql = "SELECT {$filds} FROM blues_luckydraw_orders WHERE isDel<>1  {$where}  ORDER BY createtime DESC {$limit} ";

        $query = $this->db->query($sql);
        $result =$query->result_array();
        return $result;
    }



    public function getOrderCnt($data){
		$where = " ";
		if(!empty($data['starttime'])){
            $where .= " AND createtime >= {$data['starttime']}";
        }
        if(!empty($data['endtime'])){
            $data['endtime'] = $data['endtime']+3600*24;
            $where .= " AND createtime <= {$data['endtime']}";
        }
        if(!empty($data['keyword'])){
            $where .= " AND goodsName like '%{$data['keyword']}%'  OR mobile like '%{$data['keyword']}%' OR receiver like '%{$data['keyword']}%'";
        }
		$sql = "SELECT count(*) cnt FROM blues_luckydraw_orders WHERE isDel<>1 {$where}";

        $query = $this->db->query($sql);
        $result =$query->row_array();
        return $result['cnt'];
    }
    public function get_order_by_id($id){
        $this->db->where("id",$id);
        $query=$this->db->get("blues_luckydraw_orders");
        return $query->row_array();
    }

    public function del_order($id){
        $this->db->where("id",$id);
        $res = $this->db->update("blues_luckydraw_orders",array('isDel'=>1));
        return $res;
    }

  

     //编辑状态
    public function setOrderStatus($id,$status){
        $this->db->where("id",$id);
        $data = array(
            'status'=>$status
        );
        $res = $this->db->update("blues_luckydraw_orders",$data);
        return $res;
    }

    public function getChance()
    {
        $sql = "SELECT sum(chance) total FROM blues_luckydraw WHERE isDel=0 and onlinestatus =1";
        $query = $this->db->query($sql);
        return $query->row_array()['total']; 
    }
    public function getOtherChance($id)
    {
        $sql = "SELECT sum(chance) total FROM blues_luckydraw WHERE isDel=0 and onlinestatus =1 and id <>{$id}";
        $query = $this->db->query($sql);
        return $query->row_array()['total']; 
    }
	
	public function getChooseUserList($data = array(),$per_page,$limit){
        $where = " ";
        if(!empty($data['starttime'])){
            $where .= " AND point.createtime >= '{$data['starttime']}'";
        }
        if(!empty($data['endtime'])){
            $where .= " AND point.createtime <= '{$data['endtime']}'";
        }
        
        if(!empty($data['keyword'])){
            $where .= " AND username like '%{$data['keyword']}%' OR  AccountID LIKE '%{$data['keyword']}%' OR nickname LIKE '%{$data['keyword']}%'";
        }
        $limit = "LIMIT {$per_page},{$limit}";
        if($data['status']){
           $where .= " AND nianzhong.status = {$data['status']}";
           $sql = "SELECT
                    point.AccountID,count(DISTINCT(ques.newsID)) jointcnt,`users`.username,`users`.nickname,`users`.avatar,`users`.sex,`users`.age,count(DISTINCT(point.createtime)) jointdaycnt ,nianzhong.AccountID year_month_accountid,nianzhong.id year_month_id
                FROM
                    blues_member_points point,blues_question ques, blues_user users left join blues_luckydraw_year_month nianzhong on nianzhong.AccountID=users.AccountID 
                WHERE
                `users`.AccountID=point.AccountID AND ques.questionID=point.typeid AND
                    point.type = 6 AND users.Status=0
                    {$where}
                GROUP BY
                    point.AccountID
                ORDER BY
                    point.createtime DESC

                    {$limit}";
        }else{
           $sql = "SELECT
                    point.AccountID,count(DISTINCT(ques.newsID)) jointcnt,`users`.username,`users`.nickname,`users`.avatar,`users`.sex,`users`.age,count(DISTINCT(point.createtime)) jointdaycnt 
                FROM
                    blues_member_points point, blues_user users ,blues_question ques
                WHERE
                `users`.AccountID=point.AccountID AND ques.questionID=point.typeid AND
                    point.type = 6 AND users.Status=0
                    {$where}
                GROUP BY
                    point.AccountID
                ORDER BY
                    point.createtime DESC

                    {$limit}";
        }
        
        $query = $this->db->query($sql);
        return $query->result_array();
    }

    public function checkUserSelected($AccountID)
    {
        $this->db->where(["AccountID"=>$AccountID,'status'=>1]);
        $query = $this->db->get("blues_luckydraw_year_month");
        return $query->row_array();
    }


    public function getChooseUserCnt($data = array()){
        $where = " WHERE 1 ";
        if(!empty($data['starttime'])){
            $where .= " AND createtime >= {$data['starttime']}";
        }
        if(!empty($data['endtime'])){
            $where .= " AND createtime <= {$data['endtime']}+3600*24";
        }
        if(!empty($data['name'])){
            $where .= " AND username like '%{$data['keyword']}%' OR  AccountID LIKE '%{$data['keyword']}%' OR nickname LIKE '%{$data['keyword']}%'";
        }
        $sql = "SELECT
                    *
            FROM
                   blues_user   
            where 
                status<>2 ";
        $query = $this->db->query($sql);
        return $query->num_rows();
    }

    public function addLuckydrawUser($AccountID,$current_batch)
    {
        return $this->db->insert("blues_luckydraw_year_month",['AccountID'=>$AccountID,'current_batch'=>$current_batch]);
    }

    public function deleteLuckydrawUser($id)
    {
        
        if($id)
        {
            $data['id'] = $id;
        }else{
            $data['status'] = 1;
        }
        $this->db->where($data);
        return $this->db->delete("blues_luckydraw_year_month");
    }
}

/* End of file flybird/orders_model.php */
/* Location: ./application/models/orders_model.php */